//获取当前账户下的商品
$(document).ready(function () {
  // 接收页面传递的参数id
  let id = location.search.split("=")[1];

  $.ajax({
    url: "http://jx.xuzhixiang.top/ap/api/detail.php",
    type: "get",
    data: {
      id: id
    },
    success: function (res) {
      console.log(res);
      let detail = document.querySelector(".detail");
      let str = `
              <img src="${res.data.pimg}">
              <p>${res.data.pname}</p>
              <p>￥${res.data.pprice}</p>
              <span class="minus">-</span>
              <input type="text" value=1 class="num">
              <span class="plus">+</span>       
              <input type="button" data-id="${res.data.pid}" value="加入购物车" class="addBtn" />      
        `;
      detail.innerHTML = str;

      let oMinus = document.querySelector(".minus"); // 获取+
      let oPlus = document.querySelector(".plus");   // 获取-
      let oNum = document.querySelector(".num");     // 获取当前数量
      let pid = $(".addBtn").attr("data-id");        // 自定义属性：传入pid
      console.log(pid);

      oMinus.onclick = function () {
        oNum.value--;
        if (oNum.value < 1) {
          oNum.value = 1;
        }
      };
      oPlus.onclick = function () {
        oNum.value++;
      };
      oNum.onchange = function () {
        if (oNum.value < 1) {
          oNum.value = 1;
        }
      };

      $(".addBtn").click(function () {
        $.ajax({
          url: "http://jx.xuzhixiang.top/ap/api/add-product.php",
          type: "get",
          data: {
            uid: 69007,
            pid: pid,                // 传入当前商品的pid
            pnum: oNum.value,        // 传入当前商品的数量
          },
          success: function (res) {
            console.log(res);
            location.href = "../html/cart.html";
          },
        });
      });
    },
  });
});